package lianxi;

import cn.tedu.yxc.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/Yxc/insert")
public class UserInsertServerlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        // 获取请求中键名为 p 的值
        String username = req.getParameter("username");
        String id = req.getParameter("id");
        if(id !=null && !"null".equals(id)){
            String sql="UPDATE `user` SET username =? , name=? WHERE id =?";
            JdbcUtils.modify(sql,username,name,id);
            resp.sendRedirect("/Yxc/user-list.jsp");
        }
        else{
        String sql = "INSERT  INTO `user`(`name`,`password`,`username`) VALUES(?,MD5('666666'),?)";
        JdbcUtils.modify(sql,name, username);
        resp.sendRedirect("/Yxc/user-list.jsp");}
    }
}
